package controllers

import (
	"github.com/gin-gonic/gin"
	"go_wep_demo/dao/redis"
	"go_wep_demo/models"
	"strconv"
)

func AddVoteHandler(c *gin.Context) {
	//接收参数
	var err error
	//1、参数校验
	var data = new(models.ParamAddVote)
	err = c.ShouldBind(data)
	if err != nil {
		ResponseErrorWithMsg(c, CodeInvalidParam, GetErrorMsg(err))
		return
	}
	//2、业务处理
	userId,err := GetUserId(c)
	if err != nil {
		ResponseError(c,CodeNeedLogin)
		return
	}
	userIdStr := strconv.Itoa(int(userId))
	err = redis.UserVote(userIdStr,data.ArticleId, data.Score)
	if err != nil {
		ResponseErrorWithMsg(c, CodeError,err.Error())
		return
	}
	//3、返回响应
	ResponseSuccess(c, nil)
}
